﻿using System;
using System.Linq;
using System.Web;

namespace SubForum.Framework.Utilities
{
    public static class Path
    {
        public static string FixPath(HttpContext httpContext, string relativePath)
        {
            var retVal = String.Empty;
            var standardPorts = new[] { "80", "443", null };
            var standardProt = new[] { "0", null };
            var port = httpContext.Request.ServerVariables["SERVER_PORT"];
            var prot = httpContext.Request.ServerVariables["SERVER_PORT_SECURE"];
            var host = httpContext.Request.ServerVariables["SERVER_NAME"];

            port = standardPorts.Contains(port) ? "" : ":" + port;
            prot = standardPorts.Contains(prot) ? "https://" : "http://";

            return string.Format("{0}{1}{2}{3}{4}", prot, host, port, httpContext.Request.ApplicationPath, relativePath.StartsWith("/") ? relativePath.Substring(1) : relativePath);
        }
    }
}
